# 引入flask程序包
import sys
import flask as fl
from flask import render_template, request, Response
import json
sys.path.append("..") 
from dataAnalysis.util import dataAnalysisUtil
import sqlite3

app=fl.Flask(__name__)

@app.route("/index", methods=["GET", "POST"])
def index():
    li = []
    conn = sqlite3.connect("../dataStorage/zhaopin.db")
    cursor = conn.cursor()
    sql = """
        select name, abb from dictionary
    """
    cursor.execute(sql)
    for row in cursor.fetchall():
        li_item = {
            "name": row[0],
            "abb": row[1]
        }
        li.append(li_item)
    return render_template("index.html", data = li)

@app.route("/all", methods=["GET", "POST"])
def all():
    return render_template("all.html")

@app.route("/allData", methods=["GET", "POST"])
def allData():
    analysis = dataAnalysisUtil()
    return Response(json.dumps(analysis.all()), mimetype='application/json')

@app.route("/data", methods=["GET", "POST"])
def data():
    if request.method == "GET":
        table_name = request.args.get("msg")
        analysis = dataAnalysisUtil()
        data = {
            "diploma": analysis.group_diploma(table_name),
            "city": analysis.group_city(table_name),
            "corporateType": analysis.group_corporateType(table_name),
            "companySize": analysis.group_companySize(table_name),
        }
    return Response(json.dumps(data), mimetype='application/json')

@app.route("/chart", methods=["GET", "POST"])
def chart():
    return render_template("chart.html")

@app.route("/map", methods=["GET", "POST"])
def map():
    return render_template("map.html")

if __name__=="__main__":
    app.run()
    